<template>
	<view class="">
		<!-- 顶部导航 -->
		<fa-navbar title="会员中心"></fa-navbar>
		<view class="u-flex u-row-between u-p-30 u-border-bottom" v-for="(item, index) in info" :key="index">
			<view class="title text-bold" :style="[{width:item.name=='个性签名'?'600rpx':''}]">{{ item.name }}</view>
			<view class="u-flex">
				<u-avatar v-if="item.src" :src="item.src"></u-avatar>
				<view v-if="item.text">{{ item.text }}</view>
				<!-- <u-icon class="u-m-l-10" size="32" color="#999" name="arrow-right"></u-icon> -->
			</view>
		</view>
		<view class="u-p-30 u-m-t-30 u-text-center"><u-button hover-class="none" @click="goOut">退出登录</u-button></view>
		<!-- 底部导航 -->
		<fa-tabbar></fa-tabbar>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
export default {
	onLoad() {
		this.init();
	},
	data() {
		return {
			src: '',
			info: []
		};
	},
	computed: {
		...mapState(['user'])
	},
	methods: {
		init: async function() {
			//更新vuex会员信息
			let res = await this.$api.getUserIndex({
				id: this.vuex_user.id
			});
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			this.$u.vuex('vuex_user', res.data.user);
			this.$u.vuex('vuex_isAdmin', res.data.isAdmin);
			this.info = [
				{
					name: '头像',
					src: res.data.user.avatar
				},
				{
					name: '用户名',
					text: res.data.user.nickname || '匿名'
				},
				{
					name: '个性签名',
					text: res.data.user.bio || this.vuex_bio
				},
				{
					name: '余额',
					text: this.user.money.money1 || 0
				},
				{
					name: '积分',
					text: this.user.money.money2 || 0
				},
				{
					name: '连续登录',
					text: (res.data.user.successions || 0) + ' 天'
				},
				{
					name: '最长连续登录',
					text: (res.data.user.maxsuccessions || 0) + ' 天'
				},
				{
					name: '登录时间',
					text: res.data.user.logintime
				},
				{
					name: '最后登录',
					text: res.data.user.prevtime
				}
			];
		},
		goOut: async function(index) {
			//退出登录
			let res = await this.$api.getUserLogout();
			if (!res.code) {
				this.$u.toast(res.msg);
				return;
			}
			//退出成功
			this.$u.vuex('vuex_user', { avatar: '123456' }); //清除vuex_user
			this.$u.vuex('vuex_token', ''); //清除用户token
			this.$u.vuex('vuex_isAdmin', false); //清除用户管理员
			this.$Router.back(1);
		}
	}
};
</script>

<style lang="scss"></style>
